package api;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-23 08:28
 **/
public class LocalDateMethod {
    public static void main(String[] args) {
        //把一个日期字符串解析成为一个LocalDateTime对象
        // method1();
        //使用指定的日期模板获取一个日期格式化器
        // DateTimeFormatter对象
        // method2();
        // addTime();
        // period();
        duration();
    }

    private static void duration() {
        Duration duration = Duration.between(LocalDateTime.now(),
                LocalDateTime.of(2020, 10, 23, 10, 50));
        System.out.println(duration.toDays());
        System.out.println(duration.toString());
        System.out.println(duration.toHours());
    }

    private static void period() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime tenYearsLate = now.plusYears(10).plusDays(20);
        Period between = Period.between(now.toLocalDate(), tenYearsLate.toLocalDate());
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.toTotalMonths());
        System.out.println(between.getDays());

    }

    private static void addTime() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        now.plusYears(10);
        now.minusDays(10);
        System.out.println(now.withYear(2010));
        LocalDateTime days = now.minusDays(10);
        System.out.println(days);
        LocalDateTime plusDays = now.plusDays(10);
        System.out.println(plusDays);
    }

    private static void method2() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(dtf);
        System.out.println(format);
    }

    private static void method1() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2020-10-23 08:46:15", formatter);
        System.out.println(parse);
    }

    public static void main1(String[] args) {

        //

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        System.out.println(now.getDayOfMonth());
        System.out.println(now.getMonth());
        System.out.println(now.getMonth().getValue());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfWeek());
        System.out.println(now.getDayOfWeek().getValue());
        System.out.println(now.getDayOfMonth());

        //转换成LocalDate和LocalTime
        LocalDate localDate = now.toLocalDate();
        LocalTime localTime = now.toLocalTime();
        System.out.println(localDate);
        System.out.println(localTime);

        //格式化
        System.out.println("====");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String format = now.format(formatter);
        System.out.println(format);

        LocalDateTime parse = LocalDateTime.parse("2020-10-23", formatter);
        System.out.println(parse);
    }
}
